package hiar.hiscene.com.mysocket;

import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by user on 2018/5/8.
 */

public class MySocketClient {
    private MySocketListener mListener = null;
    private MySocketClientThread mClientThread = null;

    public void startClient(String host, int port, MySocketListener listener) {
        mClientThread = new MySocketClientThread(host, port, listener);
        mClientThread.start();
    }

    public void send(String msg) {
        mClientThread.sendMessage(msg);
    }

    public class MySocketClientThread extends Thread {

        private MySocketListener mListener = null;
        private String mHost = null;
        private int mPort = 8888;
        private Socket mSocket = null;
        private volatile String mMsg = "";


        public MySocketClientThread(String host, int port, MySocketListener listener) {
            mHost = host;
            mPort = port;
            mListener = listener;
        }

        public void sendMessage(String msg) {
            synchronized (this) {
                mMsg = msg;
                notify();
            }
        }

        @Override
        public void run() {
            while (true) {
                synchronized (this) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        Log.d("MySocket", "InterruptedException:" + e.getMessage());
                    }
                    try {
                        mSocket = new Socket(mHost, mPort);
                        if (mListener != null) {
                            mListener.OnConnect(true);
                        }
                        DataOutputStream writer = null;
                        try {
                            writer = new DataOutputStream(mSocket.getOutputStream());
                            writer.write(mMsg.getBytes());
                        } catch (IOException e) {
                            Log.d("MySocket", "IOException:" + e.getMessage());
                        } finally {
                            if (writer != null) {
                                try {
                                    writer.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } catch (UnknownHostException e) {
                        if (mListener != null) {
                            mListener.OnConnect(false);
                        }
                        Log.d("MySocket", "UnknownHostException:" + e.getMessage());
                    } catch (IOException e) {
                        if (mListener != null) {
                            mListener.OnConnect(false);
                        }
                        Log.d("MySocket", "IOException:" + e.getMessage());
                    } finally {
                        if (mSocket != null) {
                            try {
                                mSocket.close();
                            } catch (IOException e) {

                            }
                            mSocket = null;
                        }
                    }

                }
            }
        }
    }

}
